package com.colorlight.cloud.complete.entity.enumerate;

import com.colorlight.cloud.common.exception.ParameterException;
import com.colorlight.cloud.common.model.ExceptionEnum;
import com.colorlight.cloud.gateway.api.message.constraint.basics.ienum.EnumTrans;
import com.colorlight.iot.proto.complete.entity.CompleteEntity;
import lombok.Getter;

import java.util.Arrays;
import java.util.Objects;

@Getter
public enum UpdateType implements EnumTrans<CompleteEntity.UpdateType, UpdateType> {
    /**
     * UPDATE_DEFAULT 默认
     * UPDATE_TYPE_PLAYER 更新播放盒
     */
    UPDATE_DEFAULT(CompleteEntity.UpdateType.UPDATE_DEFAULT),
    UPDATE_TYPE_PLAYER(CompleteEntity.UpdateType.UPDATE_TYPE_PLAYER),
    ;

    private final Integer id;
    private final String name;
    private final CompleteEntity.UpdateType updateType;

    UpdateType(CompleteEntity.UpdateType updateType) {
        this.id = updateType.getNumber();
        this.name = updateType.name();
        this.updateType = updateType;
    }

    /**
     * @param updateType
     * @return 对应的类型
     */
    public static UpdateType getUpdateType(CompleteEntity.UpdateType updateType) {
        Objects.requireNonNull(updateType, "updateType can not be null.");
        return Arrays.stream(UpdateType.values())
                .filter(e -> e.name.equals(updateType.name()))
                .findFirst()
                .orElseThrow(() -> new ParameterException(ExceptionEnum.NOT_EXIST_ENUM));
    }

    @Override
    public CompleteEntity.UpdateType toProto() {
        return this.updateType;
    }

    @Override
    public UpdateType formProto(CompleteEntity.UpdateType updateType) {
        return getUpdateType(updateType);
    }
}

